/**
 * 业务接口，具体参数请参照
 * http://note.youdao.com/group/#/10782625/office/135218187
 *
 */
import Vue from 'vue';
import VueResource from 'vue-resource';
import * as ApiTools from 'open-vue-components/src/utils/api';

Vue.use(VueResource);
// eslint-disable-next-line no-undef
const host = MAIN_HOST;
// eslint-disable-next-line no-undef
const uploadOpen = UPLOAD_OPEN;
Vue.http.options.emulateJSON = true;
export const conf = {
  /**
   * 公告列表
   */
  noticeList: {
    method: 'jsonp',
    // prod: () => `${host}/open/media/notice/list.do`
    prod: () => ({
      url: 'http://open.163.com/special/open_pgc_notice_list/',
      cbName: 'callback',
      cbFn: 'OPEN_PGC_NOTICE_LIST',
      cache: false
    })
  },
  /**
   * 上传图片
   */
  uploadImg: {
    method: 'post',
    prod: () => `${host}/photo/upload.do`,
    // dev: () => `http://test.c.open.163.com/photo/upload.do`,
    fallBack: ({ callback }) => `${host}/photo/upload.do?callback=${callback}`
  },
  /**
   * 上传音频
   */
  uploadAudio: {
    method: 'post',
    prod: ({ md5 }) => `${uploadOpen}/uploader/audio/upload.do?md5=${md5}`,
    fallBack: ({ md5, callback }) => `${uploadOpen}/uploader/audio/upload.do?md5=${md5}&callback=${callback}`,
    sliceAPI: {
      check: {
        method: 'get',
        url: `${uploadOpen}/uploader/audio/checkStatus.do`
      },
      upload: {
        method: 'post',
        url: `${uploadOpen}/uploader/audio/upload.do`
      }
    }
  },
  /**
   * 上传视频
   */
  uploadVideo: {
    method: 'post',
    // dev: ({ md5 }) => `${host}/uploader/nochunked/upload.do?md5=${md5}`,
    prod: ({ md5 }) => `${uploadOpen}/uploader/nochunked/upload.do?md5=${md5}`,
    fallBack: ({ md5, callback }) => `${uploadOpen}/uploader/nochunked/upload.do?md5=${md5}&callback=${callback}`,
    sliceAPI: {
      check: {
        method: 'get',
        url: `${uploadOpen}/uploader/checkStatus.do`
      },
      upload: {
        method: 'post',
        url: `${uploadOpen}/uploader/noinit/upload.do`
      }
    }
  },
  /**
   * 用户信息接口(获取用户信息,并判断是否是专栏用户)
   */
  userInfo: {
    method: 'get',
    prod: () => `${host}/open/specialColumn/userInfo.do`
  },
  /**
   * 专栏创建与修改
   */
  saveSpecialColumn: {
    method: 'post',
    prod: () => `${host}/open/specialColumn/saveSpecialColumn.do`
  },
  /**
   * 课程包创建
   */
  saveCoursePacket: {
    method: 'post',
    prod: () => `${host}/open/specialColumn/saveCoursePacket.do`
  },
  /**
   * 视频创建与修改
   */
  saveVideo: {
    method: 'post',
    prod: () => `${host}/open/specialColumn/saveVideo.do`
  },
  /**
   * 音频创建与修改
   */
  saveAudio: {
    method: 'post',
    prod: () => `${host}/open/specialColumn/saveAudio.do`
  },
  /**
   * 文章创建与修改
   */
  saveArticle: {
    method: 'post',
    prod: () => `${host}/open/specialColumn/saveArticle.do`
  },
  /**
   * 删除内容
   */
  deleteContent: {
    method: 'get',
    prod: () => `${host}/open/specialColumn/delete.do`
  },
  /**
   * 音频详情
   */
  audioInfo: {
    method: 'get',
    prod: () => `${host}/open/specialColumn/audioInfo.do`
  },
  /**
   * 视频详情
   */
  videoInfo: {
    method: 'get',
    prod: () => `${host}/open/specialColumn/videoInfo.do`
  },
  /**
   * 文章详情
   */
  articleInfo: {
    method: 'get',
    prod: () => `${host}/open/specialColumn/articleInfo.do`
  },
  /**
   * 专栏详情
   */
  specialColumnInfo: {
    method: 'get',
    prod: () => `${host}/open/specialColumn/specialColumnInfo.do`
  },
  /**
   * 课程包详情
   */
  coursePacketInfo: {
    method: 'get',
    prod: () => `${host}/open/specialColumn/coursePacketInfo.do`
  },
  /**
   * 专栏列表
   */
  specialColumnList: {
    method: 'get',
    prod: () => `${host}/open/specialColumn/specialColumnList.do`
  },
  /**
   * 课程包内容列表
   */
  contentList: {
    method: 'get',
    prod: () => `${host}/open/specialColumn/contentList.do`
  },
  /**
   * 内容评论列表
   */
  commentList: {
    method: 'get',
    prod: () => `${host}/open/specialColumn/commentList.do`
  },
  /**
   * 显示或隐藏评论
   */
  showOrHideComment: {
    method: 'post',
    prod: ({ ids }) => {
      let url = `${host}/open/specialColumn/showOrHideComment.do?`;
      url += ids.reduce((pre, cur) => {
        pre && (pre += '&');
        pre += `ids=${cur}`;
        return pre;
      }, '');
      //console.log(url);
      return url;
    }
  },
  subscribeStatistics: {
    method: 'get',
    prod: () => `${host}/open/specialColumn/subscribeStatistics.do`
  }
};

/**
 * 请求指定接口
 * @param api
 * @param params 包含url{}和body两个参数，对于post请求body可以为FormData
 * @returns Promise
 */
export function fetch(api, params = { url: {}, body: {}, headers: {}}) {
  return ApiTools.fetchData(conf[api],params);
}

/**
 * 上传媒体到一个api，会自动对FormData做兼容性处理
 * @param api
 * @param form 一个表单元素
 * @param option query参数、progress回调之类
 */
export function postMedia(api, form, option = {}) {
  return ApiTools.postMedia(conf[api],form,option);
}
